# Makefile for Skeleton DB for Tokyo Cabinet



#================================================================
# Setting Variables
#================================================================


# Generic settings
SHELL = @SHELL@

# Package information
PACKAGE = @PACKAGE_NAME@
VERSION = @PACKAGE_VERSION@
PACKAGEDIR = $(PACKAGE)-$(VERSION)
PACKAGETGZ = $(PACKAGE)-$(VERSION).tar.gz
LIBVER = @MYLIBVER@
LIBREV = @MYLIBREV@
PROTVER = @MYPROTVER@

# Targets
HEADERFILES = @MYHEADERFILES@
LIBRARYFILES = @MYLIBRARYFILES@
LIBOBJFILES = @MYLIBOBJFILES@
COMMANDFILES = @MYCOMMANDFILES@
INITFILES = @MYINITFILES@
MAN1FILES = @MYMAN1FILES@
MAN3FILES = @MYMAN3FILES@
MAN8FILES = @MYMAN8FILES@
SCREXTFILES = @MYSCREXTFILES@
DOCUMENTFILES = @MYDOCUMENTFILES@
PCFILES = @MYPCFILES@

# Install destinations
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
INCLUDEDIR = @includedir@
LIBDIR = @libdir@
BINDIR = @bindir@
SBINDIR = @sbindir@
LIBEXECDIR = @libexecdir@
DATADIR = @datadir@/$(PACKAGE)
MAN1DIR = @mandir@/man1
MAN3DIR = @mandir@/man3
MAN8DIR = @mandir@/man8
PCDIR = @libdir@/pkgconfig
DESTDIR =

# Building configuration
CC = @CC@
CPPFLAGS = @MYCPPFLAGS@ \
  -D_TT_PREFIX="\"$(prefix)\"" -D_TT_INCLUDEDIR="\"$(INCLUDEDIR)\"" \
  -D_TT_LIBDIR="\"$(LIBDIR)\"" -D_TT_BINDIR="\"$(BINDIR)\"" -D_TT_LIBEXECDIR="\"$(LIBEXECDIR)\"" \
  -D_TT_APPINC="\"-I$(INCLUDEDIR)\"" -D_TT_APPLIBS="\"-L$(LIBDIR) @LIBS@\""
CFLAGS = @MYCFLAGS@
LDFLAGS = @MYLDFLAGS@
CMDLDFLAGS = @MYCMDLDFLAGS@
LIBS = @LIBS@
LDENV = LD_RUN_PATH=/lib:/usr/lib:$(LIBDIR):$(HOME)/lib:/usr/local/lib:@MYRUNPATH@:.
RUNENV = @MYLDLIBPATHENV@=.:/lib:/usr/lib:$(LIBDIR):$(HOME)/lib:/usr/local/lib:@MYRUNPATH@
POSTCMD = @MYPOSTCMD@



#================================================================
# Suffix rules
#================================================================


.SUFFIXES :
.SUFFIXES : .c .o

.c.o :
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $<



#================================================================
# Actions
#================================================================


all : $(LIBRARYFILES) $(COMMANDFILES)
	@$(POSTCMD)
	@printf '\n'
	@printf '#================================================================\n'
	@printf '# Ready to install.\n'
	@printf '#================================================================\n'


clean :
	rm -rf $(LIBRARYFILES) $(LIBOBJFILES) $(COMMANDFILES) \
	  *.o *.a a.out check.in check.out gmon.out *.vlog words.tsv \
	  casket casket-* casket.* *.tch *.tcb *.tcf *.tct *.idx.* *.pyc \
	  *.ulog ulog 1978* 1979* *.rts *.pid *~ hoge moge tako ika 
	-@cd ./tests		;$(MAKE) clean

version :
	vernum=`expr $(LIBVER)00 + $(LIBREV)` ; \
	  sed -e 's/_TCSKELDB_VERSION.*/_TCSKELDB_VERSION    "$(VERSION)"/' \
	    -e "s/_TCSKELDB_LIBVER.*/_TCSKELDBLIBVER     $$vernum/" \
	    -e 's/_TCSKELDB_PROTVER.*/_TCSKELDB_PROTVER    "$(PROTVER)"/' myconf.h > myconf.h~
	[ -f myconf.h~ ] && mv -f myconf.h~ myconf.h


untabify :
	ls *.c *.h | while read name ; \
	  do \
	    sed -e 's/\t/        /g' -e 's/ *$$//' $$name > $$name~; \
	    [ -f $$name~ ] && mv -f $$name~ $$name ; \
	  done


install :
	mkdir -p $(DESTDIR)$(INCLUDEDIR)
	cp -Rf $(HEADERFILES) $(DESTDIR)$(INCLUDEDIR)
	mkdir -p $(DESTDIR)$(LIBDIR)
	cp -Rf $(LIBRARYFILES) $(DESTDIR)$(LIBDIR)
	mkdir -p $(DESTDIR)$(BINDIR)
	cp -Rf $(COMMANDFILES) $(DESTDIR)$(BINDIR)
	mkdir -p $(DESTDIR)$(SBINDIR)
	cp -Rf $(INITFILES) $(DESTDIR)$(SBINDIR)
	mkdir -p $(DESTDIR)$(DATADIR)
	cp -Rf $(SCREXTFILES) $(DOCUMENTFILES) $(DESTDIR)$(DATADIR)
	mkdir -p $(DESTDIR)$(MAN1DIR)
	cd man && cp -Rf $(MAN1FILES) $(DESTDIR)$(MAN1DIR)
	mkdir -p $(DESTDIR)$(MAN3DIR)
	cd man && cp -Rf $(MAN3FILES) $(DESTDIR)$(MAN3DIR)
	mkdir -p $(DESTDIR)$(MAN8DIR)
	cd man && cp -Rf $(MAN8FILES) $(DESTDIR)$(MAN8DIR)
	mkdir -p $(DESTDIR)$(PCDIR)
	cp -Rf $(PCFILES) $(DESTDIR)$(PCDIR)
	@printf '\n'
	@printf '#================================================================\n'
	@printf '# Thanks for using Skeleton DB for Tokyo Cabinet.\n'
	@printf '#================================================================\n'


install-strip :
	make DESTDIR=$(DESTDIR) install
	cd $(DESTDIR)$(BINDIR) && strip $(MYCOMMANDS)


uninstall :
	cd $(DESTDIR)$(INCLUDEDIR) && rm -f $(HEADERFILES)
	cd $(DESTDIR)$(LIBDIR) && rm -f $(LIBRARYFILES)
	cd $(DESTDIR)$(BINDIR) && rm -f $(COMMANDFILES)
	cd $(DESTDIR)$(SBINDIR) && rm -f $(INITFILES)
	cd $(DESTDIR)$(MAN1DIR) && rm -f $(MAN1FILES)
	cd $(DESTDIR)$(MAN3DIR) && rm -f $(MAN3FILES)
	cd $(DESTDIR)$(MAN8DIR) && rm -f $(MAN8FILES)
	rm -rf $(DESTDIR)$(DATADIR)
	cd $(DESTDIR)$(PCDIR) && rm -f $(PCFILES)

tag : distclean log
	-@cd ..                       ;svn copy ./trunk/ tags/$(PACKAGEDIR)
	-@cd ../tags/$(PACKAGEDIR)    ;svn rm svn2log.py;svn rm qp_xml.py ;autoreconf
	-@cd ../tags/$(PACKAGEDIR)    ;svn ci -m "+[tag] tagged version: $(PACKAGEDIR)" 

# u should cd the tags/xxxx/ folder to make dist
dist :
	make version
	make untabify
	make distclean
	cd .. && tar cvf - $(PACKAGEDIR) --exclude ".*/*" --exclude ".*" | gzip -c > $(PACKAGETGZ)
	sync ; sync


distclean : clean
	rm -rf Makefile tcskeldb.pc config.cache config.log config.status autom4te.cache

check : all
	-@cd tests        ;$(MAKE) check 
	@printf '\n'
	@printf '#================================================================\n'
	@printf '# Checking completed.\n'
	@printf '#================================================================\n'

log : 
	svn -v --xml log | python svn2log.py 

check-valgrind :
	make RUNCMD="valgrind --tool=memcheck --log-file=%p.vlog" check
	grep ERROR *.vlog | grep -v ' 0 errors' ; true
	grep 'at exit' *.vlog | grep -v ' 0 bytes' ; true


check-forever :
	while true ; \
	  do \
	    make check || break ; \
	  done


words :
	cat -n /usr/share/dict/words | \
	  sed -e 's/^ *//' -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \
	    -e 's/^\([0-9]*\)\t\(.*\)/\2\t\1/' > words.tsv
	./tcrmgr importtsv localhost words.tsv


.PHONY : all clean install check



#================================================================
# Building binaries
#================================================================


libtcskelbdb.a : $(LIBOBJFILES)
	$(AR) $(ARFLAGS) $@ $(LIBOBJFILES)

tcskelbdb.so : tcskelbdb.o
	$(CC) $(CFLAGS) -shared -o $@ $< $(LDFLAGS) $(LIBS)


#tcskeldb.so : tcskeldb.o
#	$(CC) $(CFLAGS) -shared -o $@ $< $(LDFLAGS) $(LIBS)


tcskelbdb.bundle : tcskelbdb.o
	$(CC) $(CFLAGS) -bundle -flat_namespace -undefined suppress -o $@ $< $(LDFLAGS) $(LIBS)


#tcskeldb.bundle : tcskeldb.o
#	$(CC) $(CFLAGS) -bundle -flat_namespace -undefined suppress -o $@ $< $(LDFLAGS) $(LIBS)

tcbdbmgr : tcbdbmgr.o  $(LIBRARYFILES)
	$(LDENV) $(CC) $(CFLAGS) -o $@ $< $(LIBOBJFILES) $(LDFLAGS) $(CMDLDFLAGS) $(LIBS)

myconf.o : myconf.h

#tcjson.o: tcjson.h

tcskelbdb.o : myconf.h

tcskeldb.o : myconf.h

tcbdbmgr.o: myconf.h tcskelbdb.h

# END OF FILE
